package MicroRpc.framework.protocol.http;

import MicroRpc.framework.beans.Invoker;
import MicroRpc.framework.beans.Url;
import MicroRpc.framework.tools.IO.IOtool;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpClient {

    public static Object send(Url url0, Invoker invoker){

        try {
            URL url=new URL("http",url0.getHost(), url0.getPort(), HttpServer.getContextPath());
            HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();

            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);

            OutputStream outputStream = httpURLConnection.getOutputStream();
            IOtool.write(invoker,outputStream);

            InputStream inputStream = httpURLConnection.getInputStream();
            if (inputStream==null){
                return null;
            }

            Object result=IOtool.read(inputStream);

            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
